package online_exams.controller;

import cn.dev33.satoken.secure.BCrypt;
import cn.dev33.satoken.stp.StpUtil;
import online_exams.domain.TUserEntity;
import online_exams.service.TUserService;
import online_exams.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户信息表(TUser)表控制层
 *
 * @author makejava
 * @since 2024-12-19 16:49:59
 */
@RestController
@RequestMapping("/tUser")
public class TUserController {
    /**
     * 服务对象
     */
    @Autowired
    private TUserService tUserService;

    /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    public Result getById(@RequestBody TUserEntity user){
//        System.out.println(user);
       TUserEntity logins= tUserService.login(user);
       if (logins.getIdNumber()==null){
           return Result.error("用户不存在");
       }
        if (BCrypt.checkpw(user.getPassword(), logins.getPassword())) {
            StpUtil.login(logins.getId());
            // 正确设置角色
            StpUtil.getSession().set("role", logins.getRole());
            // 设置权限
            if ("admin".equals(logins.getRole())) {
                StpUtil.getSession().set("permission", "admin");
            }
            // 创建Map对象
            Map<String, Object> map = new HashMap<>();
            // 将用户信息添加到Map中
            map.put("role", logins.getRole());
            map.put("realname", logins.getRealName());
            map.put("token", StpUtil.getTokenValue());
            return Result.success(map);
        }
        return Result.error("用户名或密码错误");
    }


    /**
     * 检查是否登录
     * @return
     */
    @PostMapping("/inspectlogin")
    public Result inspectlogin() {
//        System.out.println(StpUtil.isLogin());
        return Result.success(StpUtil.isLogin());
    }

    /**
     * 注册
     * @param TUserEntity 实体对象
     * @return 新增结果
     */
    @PutMapping("/Userinsert")
    public Result insert(@RequestBody TUserEntity tUserEntity) {
        TUserEntity StudentId= tUserService.GetStudentId(tUserEntity.getIdNumber());
        if (StudentId!=null){
            return Result.error("学号已存在");
        }
        tUserEntity.setRole("student");
        Integer add= tUserService.studentinsert(tUserEntity);
        if (add==1){
            return Result.success("注册成功");
        }
            return Result.error("注册失败");
    }

    /**
     * 查询用户信息
     * @return
     */
    @GetMapping("/userinfo")
    public Result getUserInfo(){
//        System.out.println("收到查询用户信息请求");
        TUserEntity login = tUserService.GetUserInfo(StpUtil.getLoginId());
        // 创建Map对象
        Map<String, Object> map = new HashMap<>();
        // 将用户信息添加到Map中
        map.put("realname", login.getRealName());
        map.put("role", login.getRole());
        map.put("idnumber", login.getIdNumber());
        map.put("email", login.getEmail());
        map.put("phone", login.getPhone());
        map.put("token", StpUtil.getTokenValue());
        return Result.success(map);
    }


}

